package com.penghk.springbootdemo.kryo;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.penghk.springbootdemo.entity.User;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

public class LearnKryo {


    @Test
    public void testKryo() {

        Kryo kryo = new Kryo();
        kryo.register(User.class, 10);

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        Output output = new Output(outputStream);

        User user = new User("tom", 12);
        kryo.writeObject(output, user);
        output.close();
        byte[] obs = outputStream.toByteArray();

        ByteArrayInputStream inputStream = new ByteArrayInputStream(obs);
        Input input = new Input(inputStream);
        User users = kryo.readObject(input, User.class);
        System.out.println(users.getName());

    }

}
